<template>
	<div class="app-container">
		<jk-operation-card ref="box1" class="margin-bottom-10">
			<div class="flex-between-center-wrap">
				<div class="flex-start-center-wrap" :style="setTitleStyle">
					{{ $route.meta.title }}
				</div>
				<div class="flex-start-center-wrap">
					<el-checkbox v-model="isCustomDate" border size="small" class="item-gap" @change="onIsCustomDateChange">自定义日期</el-checkbox>
					<div v-if="!isCustomDate" class="flex-start">
						<el-radio-group v-model="dateType" size="small" class="item-gap flex-center" @change="onDateTypeChange">
							<el-radio-button
								v-for="dateItem in dateTypeList"
								:key="dateItem.id"
								:label="dateItem.id"
							>{{ dateItem.name }}</el-radio-button>
						</el-radio-group>
						<jk-select v-model="searchParams.fromShiftCode" :option-list="shiftList" size="small" placeholder="开始班次" class="search-bar-item-size1 margin-5-0 item-gap" @change="onHandleShift"></jk-select>
					</div>
					<div v-else class="flex-start">
						<jk-date-picker v-model="searchParams.fromDate" type="date" :disabled-future-time="true" :clearable="false" placeholder="班次开始日期" size="small" class="search-bar-item-size2 margin-5-0 item-gap"></jk-date-picker>
						<jk-select v-model="searchParams.fromShiftCode" :option-list="shiftList" size="small" placeholder="开始班次" class="search-bar-item-size1 margin-5-0 item-gap"></jk-select>
						<jk-date-picker v-model="searchParams.toDate" type="date" :disabled-future-time="true" :clearable="false" placeholder="班次结束日期" size="small" class="search-bar-item-size2 margin-5-0 item-gap"></jk-date-picker>
						<jk-select v-model="searchParams.toShiftCode" :option-list="shiftList" size="small" placeholder="结束班次" class="search-bar-item-size1 margin-5-0 item-gap"></jk-select>
					</div>
					<!--<jk-select v-model="searchParams.workshopId" :clearable="false" placeholder="区域" :option-list="workshopList" size="small" class="search-bar-item-size1 margin-5-0 item-gap" @change="onDateChange"></jk-select>-->
					<!-- <el-time-picker
						v-model="pickerTime"
						is-range
						value-format="HH:mm:ss"
						size="small"
						range-separator="至"
						style="width: 280px;"
						class="margin-5-0 item-gap"
						start-placeholder="班次开始时间"
						end-placeholder="班次结束时间"
					>
					</el-time-picker> -->
					<!--<jk-select v-model="searchParams.duration" placeholder="时长排序" :option-list="[{id: 'asc', name:'正序'},{id: 'desc', name:'倒序'}]" size="small" class="search-bar-item-size1 margin-5-0 item-gap"></jk-select>-->
					<jk-input v-model="searchParams.deviceNumber" class="search-bar-item-size2 margin-5-0 item-gap" placeholder="机台号" size="small" clearable @on-enter="onSearchBtn" />
					<jk-select v-model="searchParams.position" label-key-name="id" placeholder="面位" :option-list="machineFaceList" size="small" class="search-bar-item-size1 margin-5-0 item-gap"></jk-select>
					<!--<jk-input v-model="searchParams.startIngotNo" class="search-bar-item-size1 margin-5-0 item-gap" placeholder="开始锭号" size="small" clearable @blur="onIngotNumberChange($event,'startIngotNo')" />-->
					<!--<jk-input v-model="searchParams.endIngotNo" class="search-bar-item-size1 margin-5-0 item-gap" placeholder="结束锭号" size="small" clearable @blur="onIngotNumberChange($event,'endIngotNo')" />-->
					<jk-search-button @click="onSearchBtn"></jk-search-button>
				</div>
			</div>
		</jk-operation-card>

		<jk-card>
			<jk-table ref="jkTable" :span-method="mergeColMethod" border :loading="tableLoading" :data="tableData" :height="tableHeight + 20">
				<vxe-column
					v-for="(item, index) in tableColumnList"
					:key="index"
					:title="item.title"
					:align="item.align"
					:field="item.keyName"
					:sortable="item.sortable"
					:fixed="item.fixed ? item.fixed : ''"
					:min-width="item.minWidth"
					:width="item.width"
				>
					<template slot-scope="scope">
						<el-button
							v-if="item.keyName === 'deviceBrokenHeadCount'"
							size="mini"
							type="primary"
							@click="onDetailBtn(scope.row)"
						>{{ scope.row[item.keyName] || '0' }}</el-button>
						<el-button
							v-else-if="item.keyName === 'brokenHeadCountRealtime'"
							size="mini"
							type="primary"
							@click="onDetailBtn(scope.row,true)"
						>{{ scope.row[item.keyName] || '0' }}</el-button>
						<span v-else>{{ scope.row[item.keyName] || '' }}</span>
					</template>
				</vxe-column>
			</jk-table>

			<!-- 分页组件 -->
			<footer ref="box2" class="flex-end-center margin-top-10">
				<jk-page
					:page-index="searchParams.pageNo"
					:page-sizes="searchParams.pageSizes"
					:page-size="searchParams.pageSize"
					:total="totalCount"
					@on-change="onPageChange"
				/>
			</footer>
		</jk-card>
		<detail-dialog
			:search-option-config="{
				shiftList,
				machineFaceList,
			}"
			:is-real-time="isRealTime"
			:data-item="dataItem"
			:dialog-state="detailDialogState"
			@on-visible-change="onVisibleChangeOfDetailDialog"
		></detail-dialog>
	</div>
</template>

<script>
    import calHeight from '/src/mixins/cal-table-height';
    import { pageConfig, tipsBar, formatTime, formatDate } from '/src/utils/jk/jk-tool';
    import getMenuColumn from '@/mixins/getMenuColumn';
    import handlerCache from '@/mixins/handler-cache';
    import mx from '../spindleBreakageReport/mx';
    import {
        iotSpinBrokenHeadGetBrokenHeadCountPage
    } from '../../../../api/statistic/spindleReports';
    import detailDialog from './detail-dialog.vue';
    import { workforceManagementShiftGetByTime } from '@/api/schedule/shift';

    export default {
        name: 'SpindleBreakageCountReport',
        components: {
            detailDialog
        },
        mixins: [calHeight, getMenuColumn, handlerCache, mx],
        data() {
            return {
                apiItem: {
                    'page': iotSpinBrokenHeadGetBrokenHeadCountPage
                },
                detailDialogState: false,
                dataItem: {},
                scheduleShiftList: [],
                workshopList: [],
                tableLoading: true,
                tableData: [],
                totalCount: 0,
                isRealTime: false,
                pickerTime: [],
                searchParams: {
                    pageNo: pageConfig.pageNo,
                    pageSize: pageConfig.pageSize,
                    // workshopId: undefined,
                    // duration: undefined,
                    // shiftCode: undefined,
                    deviceNumber: undefined,
                    position: undefined,
                    // belongDates: [],
                    fromDate: undefined, // 开始时间
                    fromShiftCode: undefined, // 开始班制
                    toDate: undefined, // 结束时间
                    toShiftCode: undefined// 结束班制
                }
            };
        },
        // watch: {
        //     'searchParams.shiftCode': {
        //         handler(val) {
        //             const selectedShift = this.shiftList.find(i => i.id === val);
        //             if (selectedShift) {
        //                 const { beginHour, endHour } = selectedShift;
        //                 this.pickerTime = [beginHour, endHour]; // 直接赋值
        //             }
        //         },
        //         immediate: true
        //     }
        // },
        async  mounted() {
            await this.getCurrentShiftInfoRequest();
            await this.getDependentData();
        },
        methods: {
            onDetailBtn(row, isRealTime) {
                this.isRealTime = isRealTime;
                this.dataItem = { ...row };
                this.dataItem.position = isRealTime ? row.position : '';

                // this.dataItem = { ...JSON.parse(JSON.stringify(this.searchParams)), ...row };
                // this.dataItem = JSON.parse(JSON.stringify(this.searchParams));
                // this.dataItem.deviceNumber = row.deviceNumber;
                // this.dataItem.shiftCode = row.shiftCode;
                // this.dataItem.shiftName = row.shiftName;
                this.detailDialogState = true;
            },
            onVisibleChangeOfDetailDialog(e) {
                this.detailDialogState = e;
            },
            mergeColMethod({ row, _rowIndex, column, visibleData }) {
                if (column.property === 'deviceNumber' && row.deviceNumber) {
                    const prevRow = visibleData[_rowIndex - 1];
                    let nextRow = visibleData[_rowIndex + 1];
                    if (prevRow && prevRow.deviceNumber === row.deviceNumber) {
                        return { rowspan: 0, colspan: 0 };
                    } else {
                        let countRowspan = 1;
                        while (nextRow && nextRow.deviceNumber === row.deviceNumber) {
                            nextRow = visibleData[++countRowspan + _rowIndex];
                        }
                        if (countRowspan > 1) {
                            return { rowspan: countRowspan, colspan: 1 };
                        }
                    }
                } else if (column.property === 'belongDate' && row.belongDate) {
                    const prevRow = visibleData[_rowIndex - 1];
                    let nextRow = visibleData[_rowIndex + 1];
                    if (prevRow && prevRow.deviceNumber === row.deviceNumber && prevRow.belongDate === row.belongDate) {
                        return { rowspan: 0, colspan: 0 };
                    } else {
                        let countRowspan = 1;
                        while (nextRow && nextRow.deviceNumber === row.deviceNumber && nextRow.belongDate === row.belongDate) {
                            nextRow = visibleData[++countRowspan + _rowIndex];
                        }
                        if (countRowspan > 1) {
                            return { rowspan: countRowspan, colspan: 1 };
                        }
                    }
                } else if (column.property === 'shiftName' && row.shiftName) {
                    const prevRow = visibleData[_rowIndex - 1];
                    let nextRow = visibleData[_rowIndex + 1];
                    if (prevRow && prevRow.deviceNumber === row.deviceNumber && prevRow.belongDate === row.belongDate && prevRow.shiftName === row.shiftName) {
                        return { rowspan: 0, colspan: 0 };
                    } else {
                        let countRowspan = 1;
                        while (nextRow && nextRow.deviceNumber === row.deviceNumber && nextRow.belongDate === row.belongDate && nextRow.shiftName === row.shiftName) {
                            nextRow = visibleData[++countRowspan + _rowIndex];
                        }
                        if (countRowspan > 1) {
                            return { rowspan: countRowspan, colspan: 1 };
                        }
                    }
                } else if (column.property === 'screenShiftName' && row.screenShiftName) {
                    const prevRow = visibleData[_rowIndex - 1];
                    let nextRow = visibleData[_rowIndex + 1];
                    if (prevRow && prevRow.deviceNumber === row.deviceNumber && prevRow.belongDate === row.belongDate && prevRow.shiftName === row.shiftName && prevRow.screenShiftName === row.screenShiftName) {
                        return { rowspan: 0, colspan: 0 };
                    } else {
                        let countRowspan = 1;
                        while (nextRow && nextRow.deviceNumber === row.deviceNumber && nextRow.belongDate === row.belongDate && nextRow.shiftName === row.shiftName && nextRow.screenShiftName === row.screenShiftName) {
                            nextRow = visibleData[++countRowspan + _rowIndex];
                        }
                        if (countRowspan > 1) {
                            return { rowspan: countRowspan, colspan: 1 };
                        }
                    }
                } else if (column.property === 'deviceBrokenHeadCount') {
                    const prevRow = visibleData[_rowIndex - 1];
                    let nextRow = visibleData[_rowIndex + 1];
                    if (prevRow && prevRow.deviceNumber === row.deviceNumber && prevRow.belongDate === row.belongDate && prevRow.deviceBrokenHeadCount === row.deviceBrokenHeadCount) {
                        return { rowspan: 0, colspan: 0 };
                    } else {
                        let countRowspan = 1;
                        while (nextRow && nextRow.belongDate === row.belongDate && nextRow.deviceBrokenHeadCount === row.deviceBrokenHeadCount && nextRow.deviceNumber === row.deviceNumber) {
                            nextRow = visibleData[++countRowspan + _rowIndex];
                        }
                        if (countRowspan > 1) {
                            return { rowspan: countRowspan, colspan: 1 };
                        }
                    }
                }
            },
            onDateChange() {
                // this.$set(this.searchParams, 'shiftCode', '');
                // this.scheduleShiftList = [];
            },
            onPageChange(e, size) {
                this.$set(this.searchParams, 'pageNo', e);
                this.$set(this.searchParams, 'pageSize', size);
                this.getListRequest();
            },
            clearCacheMethod() {
                // Object.assign(this.$data, this.$options.data());
                this.$nextTick(() => {
                    this.getDependentData();
                });
            },
            /** 查询列表 */
            getListRequest() {
                this.tableData = [];
                const { fromDate,
                        toDate, deviceNumber } = this.searchParams;
                if (fromDate && toDate) {
                    this.tableLoading = true;
                    const params = JSON.parse(JSON.stringify(this.searchParams));
                    // params.belongDates = this.formatBelongDates(params.belongDates, params.shiftCode, this.pickerTime);
                    params.deviceNumber = deviceNumber && deviceNumber.trim();
                    return this.apiItem['page'](params).then(res => {
                        this.tableLoading = false;
                        if (res.data.status === 200) {
                            this.tableData = res.data.res;
                            this.totalCount = res.data.count;
                        }
                    });
                } else {
                    tipsBar(this, 'custom', 'warning', '日期不能为空!');
                }
            }
        }
    };
</script>
<style>
</style>
